/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: worker_discovery.hpp
 */

#ifndef _WORKER_DISCOVERY_HPP_
#define _WORKER_DISCOVERY_HPP_

#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <vector>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/thread/mutex.hpp>

#include "runner.hpp"
#include "worker_descriptor.hpp"
#include "task_descriptor.hpp"

//-----------------------------------------------------------------------------
// Enum, struct, aliases
namespace middleware {
/*!
 * Network configuration.
 */
typedef struct {
	std::string disc_ip_toman_bind; /* IP to manager (not used to bind) */
	std::string disc_port_toman_bind; /* Port to manager (bind) */
	std::string disc_ip_toworkers_bind; /* Port to workers (not used to bind) */
	std::string disc_port_toworkers_bind; /* Port to workers (bind) */
} DiscoveryConfig;
}
//-----------------------------------------------------------------------------
// Constants
namespace middleware {
/*!
 * Min port to which the Manager will listen for incoming requests.
 */
const unsigned int kWorkerDiscoveryMinManPort = 1051;
/*!
 * Limiting port number to 2^16 - 1.
 */
const unsigned int kWorkerDiscoveryMaxManPort = 65536 - 1;
/*!
 * Etiquette for output.
 */
const std::string kWorkerDiscoveryEtq = "DISC: ";
const std::string kWorkerDiscovery1Etq = "WorkerStatusManager: ";
}
//-----------------------------------------------------------------------------
// Class definitions
namespace middleware {
class WorkerDiscovery : public Runner {
public:
	/*!
	 * Constructus a new, non-configured (invalid port), WorkerDiscovery.
	 */
	WorkerDiscovery();
	/*!
	 * Constructus a new, well-configured WorkerDiscovery.
	 */
	WorkerDiscovery(DiscoveryConfig conf);
	/*!
	 * Destroy WorkerDiscovery.
	 */
	~WorkerDiscovery();
	/*!
	 * Runs Discovery.
	 */
	void run() {
		this->_disc_thread = boost::shared_ptr<boost::thread>(
				new boost::thread(
						boost::bind(&WorkerDiscovery::exec, this)));
	}
	/*!
	 * Join this thread and wait for it to finish.
	 */
	void join() {
		(this->_disc_thread)->join();
	}
private:
	/*!
	 * Creates a casual port number > 1050 and gets it.
	 */
	std::string createPort();
	/*!
	 * Execution function.
	 */
	void exec();
	/*!
	 * Execution function for secondary thread.
	 */
	void exec_worker_status();
	/*!
	 * Function called as a thread to manage a task in order to get the proper
	 * worker. A pointer o an open stream is passed, the pointer must then
	 * be cleared properly.
	 */
	void ManageTaskDescriptor(TaskDescriptor td,
			boost::asio::ip::tcp::iostream* iostrm);
	/*!
	 * Gets a copy of the current best worker descriptor, this is a locking
	 * on on worker descriptr list mutex.
	 */
	WorkerDescriptor GetCurrentBestWorkerDescriptor();
	/*!
	 * Manage a worker descriptor after it arrives.
	 */
	void ManageWorkerDescriptor(WorkerDescriptor wd);
	/*!
	 * Initialization routine with user.
	 */
	void bonjour();
	/*!
	 * Configuration for Discovery.
	 */
	DiscoveryConfig _config;
	/*!
	 * Underlaying thread. Responsible for managing incoming requests from
	 * manager about getting a worker for a particular task.
	 */
	boost::shared_ptr<boost::thread> _disc_thread;
	/*!
	 * Undrlaying thread. Manages worker registration and status report.
	 */
	boost::shared_ptr<boost::thread> _worker_status_thread;
	/*!
	 * Mutex to guard collection of Workers.
	 */
	boost::mutex _workerdescs_monitor;
	/*!
	 * Collection of registered and updated worker_descriptors.
	 */
	std::vector<WorkerDescriptor> _workerdescs;
};
}

#endif
